/*
 * chatwidgetstore.cpp - chat widgets storage class
 * Copyright (C) 2009  Sergei Stolyarov
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Kajim::ChatInputText widget implements basic functions required
 * by chat text entry widget: history navigation, autocompletion etc
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <QtGui>

#include "chatwidgetstore.h"
#include "accountstore.h"
#include "widgets/contentcontainerwidget.h"
#include "widgets/chatwidget.h"
#include "windowstore.h"

struct Kajim::ChatWidgetStore::Private
{
    QHash<AccountContactPair, Common::ContentContainerWidget*> chats;
};

Kajim::ChatWidgetStore::~ChatWidgetStore()
{
    delete d;
}

Kajim::ChatWidgetStore * Kajim::ChatWidgetStore::instance()
{
    if (Kajim::ChatWidgetStore::inst == 0) {
        Kajim::ChatWidgetStore::inst = new Kajim::ChatWidgetStore();
    }

    return Kajim::ChatWidgetStore::inst;
}

QWidget* Kajim::ChatWidgetStore::chatWidget(int accountHandler, const QString & peerJid)
{
    Kajim::ChatWidgetStore::AccountContactPair k(accountHandler, peerJid);

    if (!d->chats.contains(k)) {
        Kajim::ChatWidget * w = new Kajim::ChatWidget(peerJid, accountHandler);
        connect(w, SIGNAL(destroyed(QObject *)), this, SLOT(destroyChatWidget(QObject *)));
        d->chats[k] = w;
        // TODO: set account and contact details into w
    }

    Kajim::WindowStore * windowStore = Kajim::WindowStore::instance();
    QWidget * chatParent = windowStore->chatWindow(d->chats[k]);
    chatParent->show();

    return d->chats[k];
}

void Kajim::ChatWidgetStore::incomingMessage(int accountHandler, const QString & peerJid, const QString & body, const QString & subject)
{
    // find corresponding chat
    Kajim::ChatWidgetStore::AccountContactPair k(accountHandler, peerJid);
    Kajim::ChatWidget * cw = 0;

    if (!d->chats.contains(k)) {
        // chat for full JID not found, try the short JID
        int pos = peerJid.indexOf(QChar('/'));
        QString shortJid = peerJid.left(pos);

        Kajim::ChatWidgetStore::AccountContactPair k(accountHandler, shortJid);
        if (!d->chats.contains(k)) {
            // TODO: remember received message
            emit chatRequired(accountHandler, peerJid);
            return;
        }
        cw = qobject_cast<Kajim::ChatWidget*>(d->chats[k]);
    } else {
        cw = qobject_cast<Kajim::ChatWidget*>(d->chats[k]);
    }

    cw->receivePeerMessage(body, subject);
}

void Kajim::ChatWidgetStore::destroyChatWidget(QObject * obj)
{
    // try to find obj in the list of chat widgets
    Kajim::ChatWidgetStore::AccountContactPair default_key;

    Kajim::ChatWidgetStore::AccountContactPair k = d->chats.key(static_cast<Kajim::Common::ContentContainerWidget*>(obj));

    if (k != default_key) {
        d->chats.remove(k);
    }
}

Kajim::ChatWidgetStore::ChatWidgetStore(QObject * parent)
    : QObject(parent)
{
    d = new Private;
}

Kajim::ChatWidgetStore * Kajim::ChatWidgetStore::inst = 0;
